package code

import (
	"container/list"
	utils "gitee.com/ichub/goconfig/common/base/baseutils/stringutils"
	"gitee.com/ichub/gofactroy/common/ichubbase"
	"gitee.com/ichub/gofactroy/factroy/dbfactroy"
	"github.com/sirupsen/logrus"
	"time"
)

type DaoFactroy struct {
	mapstat map[string]string
}

var InstDaofactroy DaoFactroy

func init() {
	InstDaofactroy.mapstat = make(map[string]string)

	InstDaofactroy.mapstat["s_default"] = `
	if pp.{{.goFieldName}} != "" {
		b,_ := strconv.ParseBool(pp.{{.goFieldName}}) //ParseBool(str string)(value bool, err error)
		dbc = dbc.Where("{{.field_name}}=?", b)
	}
`

	InstDaofactroy.mapstat["s_numeric"] = `
	if pp.{{.goFieldName}} != 0 {
		dbc = dbc.Where("{{.field_name}}=?", pp.{{.goFieldName}})
	}`

	InstDaofactroy.mapstat["s_string"] = `
	if pp.{{.goFieldName}} != "" {
		
		if  param.FuzzyQuery { 
			dbc = dbc.Where("{{.field_name}} like ?",  "%"+pp.{{.goFieldName}}+"%")
		} else{
			dbc = dbc.Where("{{.field_name}}=?", pp.{{.goFieldName}})
		}

	}`

	InstDaofactroy.mapstat["s_datetimeUTC"] = `
	if  !pp.{{.goFieldName}}.IsZero() && pp.{{.goFieldName}}.Unix() != 0 {
		dbc = dbc.Where("{{.field_name}}=?", daoInst.localTimeUTCFormat( pp.{{.goFieldName}}.Time ))
	}`
	InstDaofactroy.mapstat["s_datetime"] = `
	if  !pp.{{.goFieldName}}.IsZero() && pp.{{.goFieldName}}.Unix() != 0 {
		dbc = dbc.Where("{{.field_name}}=?", daoInst.localTimeFormat( pp.{{.goFieldName}}.Time ))
	}`
	// && !pp.Adate.IsZero()
	InstDaofactroy.mapstat["s_date"] = `
	if !pp.{{.goFieldName}}.IsZero() && pp.{{.goFieldName}}.Unix() != 0 {
		dbc = dbc.Where("{{.field_name}}=?", daoInst.localDateFormat( pp.{{.goFieldName}} ))
	}`

}

func (factry *DaoFactroy) FindStat(c dbfactroy.COLUMNS) (s string) {
	s = InstDaofactroy.mapstat["s_default"]
	if c.IfNumeric() {
		s = InstDaofactroy.mapstat["s_numeric"]
	} else if c.IfString() {
		s = InstDaofactroy.mapstat["s_string"]
	} else if c.IfLocalDateInt() {
		s = InstDaofactroy.mapstat["s_date"]
	} else if c.IfLocalTimeInt() {
		s = InstDaofactroy.mapstat["s_datetime"]
	} else if c.IfLocalTimeUTCInt() {
		s = InstDaofactroy.mapstat["s_datetimeUTC"]
	}
	return
}

func (factry *DaoFactroy) buildIf(columns *[]dbfactroy.COLUMNS) (lst *list.List) {

	lst = list.New()
	for _, v := range *columns {
		stat := factry.FindStat(v)

		vars := make(map[string]interface{})
		vars["field_name"] = v.COLUMN_NAME
		vars["goFieldName"] = utils.Case2Camel(v.COLUMN_NAME)
		wheres := utils.ParseTemplateString(stat, vars)
		if !v.IfDate() {
			logrus.Info(wheres)
			lst.PushBack(wheres)
		}
	}

	return lst
}

func (factry *DaoFactroy) buildDAO(tableTool *dbfactroy.DbFactroy) string {

	bs := dbfactroy.InstFileFactroy.ReadTemplateDAO()
	vars := make(map[string]interface{})

	vars["FileName"] = utils.Case2Camel(tableTool.Table) + "Dao.go"
	vars["Description"] = "DAO层" + utils.Case2Camel(tableTool.Table) + "Dao"
	vars["Author"] = tableTool.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(tableTool.Table)
	vars["TableName"] = tableTool.Table
	vars["pkeyField"] = tableTool.Pkey
	vars["pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindGoType(tableTool.PkeyType)

	cs := tableTool.FindCOLUMNS()
	vars["BuildWheres"] = utils.List2string(factry.buildIf(cs))

	return utils.ParseTemplateString(string(bs), vars)

}

func (factry *DaoFactroy) BuildDAOFile(dbt *dbfactroy.DbFactroy) (dir, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	dir = dbfactroy.InstFileFactroy.TransDir(dbfactroy.Gendir_dao, fbase)

	f, c = fbase+"Dao.go", factry.buildDAO(dbt)
	dbfactroy.InstFileFactroy.WriteFileModule(dir, f, c)

	return
}

func (factry *DaoFactroy) Build(dbt *dbfactroy.DbFactroy) (dir, f, c string) {
	return factry.BuildDAOFile(dbt)
}
